//class DataProvider
package baidu.ui.data
{
    import baidu.ui.events.*;
    import flash.events.*;

    public class DataProvider extends flash.events.EventDispatcher
    {
        public function DataProvider(arg1:Object=null)
        {
            super();
            if (arg1 != null)
            {
                this.data = this.getDataFromObject(arg1);
            }
            else
            {
                this.data = [];
            }
            return;
        }

        public function get length():uint
        {
            return this.data.length;
        }

        public function invalidateItemAt(arg1:int):void
        {
            this.checkIndex(arg1, (this.data.length - 1));
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.INVALIDATE, [this.data[arg1]], arg1, arg1);
            return;
        }

        public function invalidateItem(arg1:Object):void
        {
            var loc1:*;
            loc1 = this.getItemIndex(arg1);
            if (loc1 == -1)
            {
                return;
            }
            this.invalidateItemAt(loc1);
            return;
        }

        public function invalidate():void
        {
            dispatchEvent(new baidu.ui.events.DataChangeEvent(baidu.ui.events.DataChangeEvent.DATA_CHANGE, baidu.ui.events.DataChangeType.INVALIDATE_ALL, this.data.concat(), 0, this.data.length));
            return;
        }

        public function addItemAt(arg1:Object, arg2:uint):void
        {
            this.checkIndex(arg2, this.data.length);
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.ADD, [arg1], arg2, arg2);
            this.data.splice(arg2, 0, arg1);
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.ADD, [arg1], arg2, arg2);
            return;
        }

        public function addItem(arg1:Object):void
        {
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.ADD, [arg1], (this.data.length - 1), (this.data.length - 1));
            this.data.push(arg1);
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.ADD, [arg1], (this.data.length - 1), (this.data.length - 1));
            return;
        }

        public function addItemsAt(arg1:Object, arg2:uint):void
        {
            this.checkIndex(arg2, this.data.length);
            var loc1:*;
            loc1 = this.getDataFromObject(arg1);
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.ADD, loc1, arg2, (arg2 + loc1.length - 1));
            this.data.splice.apply(this.data, [arg2, 0].concat(loc1));
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.ADD, loc1, arg2, (arg2 + loc1.length - 1));
            return;
        }

        public function addItems(arg1:Object):void
        {
            this.addItemsAt(arg1, this.data.length);
            return;
        }

        public function concat(arg1:Object):void
        {
            this.addItems(arg1);
            return;
        }

        public function merge(arg1:Object):void
        {
            var loc5:*;
            loc5 = null;
            var loc1:*;
            loc1 = this.getDataFromObject(arg1);
            var loc2:*;
            loc2 = loc1.length;
            var loc3:*;
            loc3 = this.data.length;
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.ADD, this.data.slice(loc3, this.data.length), loc3, (this.data.length - 1));
            var loc4:*;
            loc4 = 0;
            while (loc4 < loc2)
            {
                loc5 = loc1[loc4];
                if (this.getItemIndex(loc5) == -1)
                {
                    this.data.push(loc5);
                }
                loc4 = (loc4 + 1);
            }
            if (this.data.length > loc3)
            {
                this.dispatchChangeEvent(baidu.ui.events.DataChangeType.ADD, this.data.slice(loc3, this.data.length), loc3, (this.data.length - 1));
            }
            else
            {
                this.dispatchChangeEvent(baidu.ui.events.DataChangeType.ADD, [], -1, -1);
            }
            return;
        }

        public function getItemAt(arg1:uint):Object
        {
            this.checkIndex(arg1, (this.data.length - 1));
            return this.data[arg1];
        }

        public function getItemIndex(arg1:Object):int
        {
            return this.data.indexOf(arg1);
        }

        public function removeItemAt(arg1:uint):Object
        {
            this.checkIndex(arg1, (this.data.length - 1));
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.REMOVE, this.data.slice(arg1, arg1 + 1), arg1, arg1);
            var loc1:*;
            loc1 = this.data.splice(arg1, 1);
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.REMOVE, loc1, arg1, arg1);
            return loc1[0];
        }

        public function removeItem(arg1:Object):Object
        {
            var loc1:*;
            loc1 = this.getItemIndex(arg1);
            if (loc1 != -1)
            {
                return this.removeItemAt(loc1);
            }
            return null;
        }

        public function removeAll():void
        {
            var loc1:*;
            loc1 = this.data.concat();
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.REMOVE_ALL, loc1, 0, loc1.length);
            this.data = [];
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.REMOVE_ALL, loc1, 0, loc1.length);
            return;
        }

        public function replaceItem(arg1:Object, arg2:Object):Object
        {
            var loc1:*;
            loc1 = this.getItemIndex(arg2);
            if (loc1 != -1)
            {
                return this.replaceItemAt(arg1, loc1);
            }
            return null;
        }

        public function replaceItemAt(arg1:Object, arg2:uint):Object
        {
            this.checkIndex(arg2, (this.data.length - 1));
            var loc1:*;
            loc1 = [this.data[arg2]];
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.REPLACE, loc1, arg2, arg2);
            this.data[arg2] = arg1;
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.REPLACE, loc1, arg2, arg2);
            return loc1[0];
        }

        public function sort(... rest):*
        {
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.SORT, this.data.concat(), 0, (this.data.length - 1));
            var loc1:*;
            loc1 = this.data.sort.apply(this.data, rest);
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.SORT, this.data.concat(), 0, (this.data.length - 1));
            return loc1;
        }

        public function sortOn(arg1:Object, arg2:Object=null):*
        {
            this.dispatchPreChangeEvent(baidu.ui.events.DataChangeType.SORT, this.data.concat(), 0, (this.data.length - 1));
            var loc1:*;
            loc1 = this.data.sortOn(arg1, arg2);
            this.dispatchChangeEvent(baidu.ui.events.DataChangeType.SORT, this.data.concat(), 0, (this.data.length - 1));
            return loc1;
        }

        public function clone():baidu.ui.data.DataProvider
        {
            return new baidu.ui.data.DataProvider(this.data);
        }

        public function toArray():Array
        {
            return this.data.concat();
        }

        public override function toString():String
        {
            return "DataProvider [" + this.data.join(" , ") + "]";
        }

        protected function getDataFromObject(arg1:*):Array
        {
            var loc1:*;
            loc1 = null;
            var loc2:*;
            loc2 = null;
            var loc3:*;
            loc3 = null;
            var loc4:*;
            loc4 = 0;
            var loc5:*;
            loc5 = null;
            var loc6:*;
            loc6 = null;
            var loc7:*;
            loc7 = null;
            var loc8:*;
            loc8 = null;
            var loc9:*;
            loc9 = null;
            var loc10:*;
            loc10 = null;
            var loc11:*;
            loc11 = null;
            if (arg1 as Array)
            {
                if ((loc3 = arg1 as Array).length > 0)
                {
                    if (loc3[0] as String || loc3[0] as Number)
                    {
                        loc1 = [];
                        loc4 = 0;
                        while (loc4 < loc3.length)
                        {
                            loc2 = loc3[loc4];
                            loc1.push(loc2);
                            loc4 = (loc4 + 1);
                        }
                        return loc1;
                    }
                }
                return loc3.concat();
            }
            if (arg1 as baidu.ui.data.DataProvider)
            {
                return (arg1 as baidu.ui.data.DataProvider).toArray();
            }
            if (arg1 as XML)
            {
                loc5 = arg1 as XML;
                loc1 = [];
                loc6 = loc5.*;
                var loc12:*;
                loc12 = 0;
                var loc13:*;
                loc13 = loc6;
                for each (loc7 in loc13)
                {
                    loc2 = {};
                    loc8 = loc7.attributes();
                    var loc14:*;
                    loc14 = 0;
                    var loc15:*;
                    loc15 = loc8;
                    for each (loc9 in loc15)
                    {
                        loc2[loc9.localName()] = loc9.toString();
                    }
                    loc10 = loc7.*;
                    loc14 = 0;
                    loc15 = loc10;
                    for each (loc11 in loc15)
                    {
                        if (!loc11.hasSimpleContent())
                        {
                            continue;
                        }
                        loc2[loc11.localName()] = loc11.toString();
                    }
                    loc1.push(loc2);
                }
                return loc1;
            }
            throw new TypeError("Error: Type Coercion failed: cannot convert " + arg1 + " to Array or DataProvider.");
        }

        protected function checkIndex(arg1:int, arg2:int):void
        {
            if (arg1 > arg2 || arg1 < 0)
            {
                throw new RangeError("DataProvider index (" + arg1 + ") is not in acceptable range (0 - " + arg2 + ")");
            }
            return;
        }

        protected function dispatchChangeEvent(arg1:String, arg2:Array, arg3:int, arg4:int):void
        {
            dispatchEvent(new baidu.ui.events.DataChangeEvent(baidu.ui.events.DataChangeEvent.DATA_CHANGE, arg1, arg2, arg3, arg4));
            return;
        }

        protected function dispatchPreChangeEvent(arg1:String, arg2:Array, arg3:int, arg4:int):void
        {
            dispatchEvent(new baidu.ui.events.DataChangeEvent(baidu.ui.events.DataChangeEvent.PRE_DATA_CHANGE, arg1, arg2, arg3, arg4));
            return;
        }

        protected var data:Array;
    }
}